package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<Contact> contactList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initContact();
        ContactAdapter adapter = new ContactAdapter(MainActivity.this, R.layout.contact_item, contactList);
        ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Contact contact = contactList.get(position);
                Toast.makeText(MainActivity.this, contact.getName(), Toast.LENGTH_SHORT).show();
                Sinformation sp = new Sinformation();
                if (contact.getName() == "He") {
                    sp.setName("何");
                    sp.setID("1");
                    sp.setRegion("福建");
                } else if (contact.getName() == "Lv") {
                    sp.setName("吕");
                    sp.setID("2");
                    sp.setRegion("上饶");
                } else if (contact.getName() == "Cai") {
                    sp.setName("蔡");
                    sp.setID("3");
                    sp.setRegion("上饶");
                }
                Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
                Intent intent = new Intent(MainActivity.this, ChatActivity.class);
                intent.putExtra("Sp", sp);
                startActivity(intent);
            }
        });
    }

    private void initContact() {
        Contact he = new Contact("He", R.drawable.he);
        contactList.add(he);
        Contact lv = new Contact("Lv", R.drawable.lv);
        contactList.add(lv);
        Contact cai = new Contact("Cai", R.drawable.cai);
        contactList.add(cai);
    }
}